package org.glorylight.hacks.social.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.glorylight.hacks.social.data.ScrapBookData;
import org.glorylight.hacks.social.model.Notification;
import org.glorylight.hacks.social.model.ScrapBook;
import org.glorylight.hacks.social.model.UserNetwork;
import org.glorylight.hacks.social.util.ModelHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class ScrapBookService extends BaseService {

	private static final Logger log = LoggerFactory.getLogger(ScrapBookService.class);
	
	@Transactional(readOnly = false)
	public void addBook(ScrapBookData book) {
		if (log.isDebugEnabled())
			log.debug("add new book for: {} with title: {}", new Object[] {book.getUserId(), book.getTitle()});
		add(ModelHelper.convertScrapBookData(book));
		if (book.isShared())
			addNotification(book);
	}
	
	@Transactional(readOnly = false)
	private void addNotification(ScrapBookData book) {
		try {
			List<UserNetwork> result = em.createNamedQuery("network-by-user", UserNetwork.class)
					.setParameter("userId", book.getUserId()).getResultList();
			log.info("{} users added for notification", result.size());
			
			for (UserNetwork un : result) {
				add(new Notification(UUID.randomUUID(), un.getConnectTo(), book.getId(), un.getId(), book.getDateCreated(), 1));
			}
		} catch (Exception e) {
			log.error("error persisting notification ..", e);
		}
	}

	@Transactional(readOnly = false)
	public void deleteBook(ScrapBookData book) {
		if (log.isDebugEnabled())
			log.debug("delete book for: " + book.getId());
		ScrapBook sbook = findBook(book.getId());
		delete(sbook);
	}
	
	@Transactional(readOnly = false)
	public ScrapBookData updateBook(ScrapBookData data) {
		log.debug("updating picture title and description");
		ScrapBook pic = findBook(data.getId());
		pic.setTitle(data.getTitle());
		pic.setDescription(data.getDesc());
		update(pic);
		return ModelHelper.convertScrapBook(pic);
	}
	
	private ScrapBook findBook(UUID id) {
		if (log.isDebugEnabled())
			log.debug("retrieve picture: {}", id);
		return em.find(ScrapBook.class, id);
	}

	public ScrapBookData find(UUID id) {
		if (log.isDebugEnabled())
			log.debug("retrieve book: " + id);
		ScrapBook book = em.find(ScrapBook.class, id);
		if (book != null)
			return ModelHelper.convertScrapBook(book);
		else
			return null;
	}

	public List<ScrapBookData> findAll() {
		if (log.isDebugEnabled())
			log.debug("find all user");
		List<ScrapBook> result = em.createQuery("from ScrapBook", ScrapBook.class).getResultList();
		List<ScrapBookData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<ScrapBookData>();
			for (ScrapBook u : result)
				datas.add(ModelHelper.convertScrapBook(u));
		}
		
		return datas;
	}
	
	public List<ScrapBookData> findAllByUser(String userId) {
		if (log.isDebugEnabled())
			log.debug("find all books");
		List<ScrapBook> result = em.createNamedQuery("books-by-user", ScrapBook.class)
				.setParameter("userId", userId).getResultList();
		
		List<ScrapBookData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<ScrapBookData>();
			for (ScrapBook u : result)
				datas.add(ModelHelper.convertScrapBook(u));
		}
		
		return datas;
	}
	
	public List<ScrapBookData> findAllByUserDate(String userId, Date startDate, Date endDate) {
		if (log.isDebugEnabled())
			log.debug("find all books by user date");
		List<ScrapBook> result = em.createNamedQuery("books-by-user-date", ScrapBook.class)
				.setParameter("userId", userId)
				.setParameter("startDate", startDate)
				.setParameter("endDate", endDate).getResultList();
		List<ScrapBookData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<ScrapBookData>();
			for (ScrapBook u : result)
				datas.add(ModelHelper.convertScrapBook(u));
		}
		
		return datas;
	}
	
	public List<ScrapBookData> findAllByDate(Date startDate, Date endDate) {
		if (log.isDebugEnabled())
			log.debug("find all books by date");
		List<ScrapBook> result = em.createNamedQuery("books-by-date", ScrapBook.class)
				.setParameter("startDate", startDate)
				.setParameter("endDate", endDate).getResultList();
		List<ScrapBookData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<ScrapBookData>();
			for (ScrapBook u : result)
				datas.add(ModelHelper.convertScrapBook(u));
		}
		
		return datas;
	}
}
